/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gprs.sever;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kienbk1910
 */
public class Server implements Runnable {
     private MainFrame ServerFrame;
     private Integer ServerPort;
     private ServerSocket severSocket;
     private Socket socket;
     private boolean running;
     private ReaderSocket  Reader; 
     public Server (MainFrame Frame,Integer Port){
         ServerFrame= Frame;
         ServerPort = Port;
     }
    @Override
    public void run() {
        try {
            //  throw new UnsupportedOperationException("Not supported yet.");
          this.severSocket= new ServerSocket(this.ServerPort);
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.running=true;
        try {
            while(running == true){
            this.socket =this.severSocket.accept();
            this.ServerFrame.setTextUserArea("connect"+"\n");
            Reader = new ReaderSocket(socket,ServerFrame);
            
            new Thread(Reader).start();
            }
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            
            this.severSocket.close();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void stop() throws IOException{
        this.running= false;
        this.severSocket.close();
    
    }
    
}
